<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增客人信息')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-guest-add">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">客人姓名：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">身份证号：</label>
                <div class="col-sm-8">
                    <input name="idNumber" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">入住房间：</label>
                <div class="col-sm-8">
                    <select name="roomId" class="form-control" required onchange="updateRoomPrice(this.value)">
                        <option th:each="room:${roomList}" th:value="${room.id}"
                                th:text="${room.number}" ></option>
                    </select>
                </div>
            </div>
        </div>
        <input name="roomPrice" id="roomPrice" type="hidden">
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">联系电话：</label>
                <div class="col-sm-8">
                    <input name="phone" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">入住时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="checkInTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required onchange="enableCheckOutTime()">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">退房时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="checkOutTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required disabled onchange="calculateTotalPrice()">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">总费用：</label>
                <div class="col-sm-8">
                    <input name="totalPrice" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-12">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">客人状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('customer_status')}">
                        <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}" required>
                        <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "system/guest"
    $("#form-guest-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-guest-add').serialize());
        }
    }

    $("input[name='checkInTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='checkOutTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    document.addEventListener('DOMContentLoaded', function () {
        var roomSelect = document.getElementsByName('roomId')[0];
        var selectedIndex = roomSelect.selectedIndex;
        var selectedValue = roomSelect.options[selectedIndex].value;
        updateRoomPrice(selectedValue);
    });

    // 新增计算总费用的函数
    function calculateTotalPrice() {
        var checkInTime = new Date($("input[name='checkInTime']").val());
        var checkOutTime = new Date($("input[name='checkOutTime']").val());
        var roomPrice = parseFloat($("input[name='roomPrice']").val());
        var timeDifferenceInDays = Math.ceil((checkOutTime - checkInTime) / (1000 * 60 * 60 * 24));
        console.log("时间差：", timeDifferenceInDays);
        var totalPrice = timeDifferenceInDays * roomPrice;
        console.log("总金额：", totalPrice);
        $("input[name='totalPrice']").val(totalPrice);
    }

    // 启用退房时间输入框
    function enableCheckOutTime() {
        $("input[name='checkOutTime']").prop('disabled', false);
    }

    // 当入住房间下拉列表更改时更新 roomPrice
    function updateRoomPrice(selectedRoomId) {
        var val = $('#roomPrice').val();
        console.log("房间单价{}", val);
        var rooms = [[${roomList}]];
        for (var i = 0; i < rooms.length; i++) {
            console.log("循环", i, "次Room ID:", rooms[i].id, "SelectedRoomID:", selectedRoomId);
            if (Number(selectedRoomId) === Number(rooms[i].id)) {
                console.log("Selected Room Price:", rooms[i].price);
                $("input[name='roomPrice']").val(rooms[i].price);
                break;
            }
        }
    }
</script>
</body>
</html>